package leetcode.code022;

import java.util.ArrayList;
import java.util.List;

public class Solution1 extends Solution022 {

	// n = 3
	// ["((()))","(()())","(())()","()(())","()()()"]

	private final char LK = '(';
	private final char RK = ')';

	@Override
	public List<String> generateParenthesis(int n) {
		List<String> ans = new ArrayList<>();
		StringBuffer temp = new StringBuffer();
		generateParenthesis(n, n, temp, ans);// n个左括号，n个右括号，组成一个temp，放到ans里。
		return ans;
	}

	private void generateParenthesis(int left, int right, StringBuffer temp, List<String> ans) {
		if (left == 0 && right == 0) {
			ans.add(temp.toString());
			return;
		}
		if (left > 0) {
			temp.append(LK);
			this.generateParenthesis(left - 1, right, temp, ans);
			temp.deleteCharAt(temp.length() - 1);
		}
		if (left < right) {
			temp.append(RK);
			this.generateParenthesis(left, right - 1, temp, ans);
			temp.deleteCharAt(temp.length() - 1);
		}
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
	}

}
